var load_data_khach_hang;
jQuery(function() {
    if (jQuery('#txt-ma-khach-hang').length) {
        load_data_khach_hang = true;
    }
    else {
        load_data_khach_hang = false;
    }
    var url = 'http://' + location.hostname + Drupal.settings.basePath;
    var ihm = parseInt(jQuery('#total-hang-mua').val());
    var ibht = parseInt(jQuery('#total-hang-bao-hanh-truoc').val());
    jQuery('#ghi-chu').keyup(function() {
        var length = jQuery(this).val().length;
        jQuery(this).parent().find('#ghi-chu-length').text(length);
    });
    jQuery('#ghi-chu').keyup();
    jQuery('#txt-khach-hang').change(function() {
        load_data_khach_hang = false;
        jQuery('#load-data-khach-hang-wrapper').html('');
        jQuery('#hdbh-fs').hide();
        jQuery('#hnc-fs').hide();
        jQuery('#form-item-hang-duoc-bao-hanh-wrapper').html('');
        jQuery('#form-item-hang-no-cu-wrapper').html('');
    });
    jQuery('.txt-ten-hang-mua').change(function() {
        jQuery(this).parent().parent().find('.txt-hidden').val('');
        jQuery(this).parent().parent().find('.data-text').html('');
    });
    jQuery('.field-remove-submit').click(function() {
        jQuery(this).parent().parent().remove();
    });
    jQuery('.txt-tra-hang-duoc-bao-hanh').click(function() {
        var mp_tra_hang = jQuery(this).val();
        if (jQuery(this).is(':checked')) {
            jQuery('.tra-hang-' + mp_tra_hang).attr('checked', true);
        }
        else {
            jQuery('.tra-hang-' + mp_tra_hang).attr('checked', false);
        }
    });
    jQuery('.tru-tien').each(function() {
        if (jQuery(this).is(':checked')) {
            jQuery(this).parent().children('.form-text').show();
        }
        else {
            jQuery(this).parent().children('.form-text').hide();
        }
    });
    jQuery('.tru-tien').click(function() {
        if (jQuery(this).is(':checked')) {
            jQuery(this).parent().children('.form-text').show();
        }
        else {
            jQuery(this).parent().children('.form-text').hide();
            jQuery(this).parent().children('.form-text').val('');
        }
    });
    jQuery('.format-currency').keyup(function() {
        var val = currencyToNumber(jQuery(this).val());
        if (isNaN(val)) {
            jQuery(this).val('');
        }
        else {
            jQuery(this).val(numberToCurrency(val));
        }
    });
    jQuery('#load-data-khach-hang').click(function() {
        var ten_khach_hang = jQuery('#txt-khach-hang').val();
        var ma_khach_hang = '';
        if (ten_khach_hang != '') {
            var last_b = ten_khach_hang.lastIndexOf('[') + 1;
            var last_e = ten_khach_hang.lastIndexOf(']');
            if (last_b >= 1 && last_e >= 1 && last_e >= last_b + 6) {
                ma_khach_hang = ten_khach_hang.substring(last_b, last_e);
            }
        }
        if (ma_khach_hang) {
            jQuery.ajax({
                type: "POST",
                data: {ma_khach_hang : ma_khach_hang},
                url: url + 'san_pham/ban_hang/phieu_ban_hang/load_khach_hang',
                async: false,
                success: function(data) {
                    if (data != '') {
                        jQuery('#load-data-khach-hang-wrapper').html(data);
                        load_data_khach_hang = true;
                    }
                }
            });
            jQuery.ajax({
                type: "POST",
                data: {ma_khach_hang : ma_khach_hang},
                url: url + 'san_pham/ban_hang/phieu_ban_hang/load_hang_bao_hanh',
                async: false,
                success: function(data) {
                    if (data != '') {
                        jQuery('#form-item-hang-duoc-bao-hanh-wrapper').html(data);
                        jQuery('#hdbh-fs').show();
                        jQuery('.txt-tra-hang-duoc-bao-hanh').click(function() {
                            var mp_tra_hang = jQuery(this).val();
                            if (jQuery(this).is(':checked')) {
                                jQuery('.tra-hang-' + mp_tra_hang).attr('checked', true);
                            }
                            else {
                                jQuery('.tra-hang-' + mp_tra_hang).attr('checked', false);
                            }
                        });
                        jQuery('.txt-tru-tien-hang-duoc-bao-hanh').click(function() {
                            if (jQuery(this).is(':checked')) {
                                jQuery(this).parent().children('.txt-don-gia-tru-tien-hang-duoc-bao-hanh').show();
                            }
                            else {
                                jQuery(this).parent().children('.txt-don-gia-tru-tien-hang-duoc-bao-hanh').hide();
                                jQuery(this).parent().children('.txt-don-gia-tru-tien-hang-duoc-bao-hanh').val('');
                            }
                        });
                        jQuery('.txt-don-gia-tru-tien-hang-duoc-bao-hanh').keyup(function() {
                            var val = currencyToNumber(jQuery(this).val());
                            if (isNaN(val)) {
                                jQuery(this).val('');
                            }
                            else {
                                jQuery(this).val(numberToCurrency(val));
                            }
                        });
                    }
                }
            });
            jQuery.ajax({
                type: "POST",
                data: {ma_khach_hang : ma_khach_hang},
                url: url + 'san_pham/ban_hang/phieu_ban_hang/load_hang_no_cu',
                async: false,
                success: function(data) {
                    if (data != '') {
                        jQuery('#form-item-hang-no-cu-wrapper').html(data);
                        jQuery('#hnc-fs').show();
                        jQuery('.txt-tru-tien-hang-no-cu').click(function() {
                            if (jQuery(this).is(':checked')) {
                                jQuery(this).parent().children('.txt-don-gia-tru-tien-hang-no-cu').show();
                            }
                            else {
                                jQuery(this).parent().children('.txt-don-gia-tru-tien-hang-no-cu').hide();
                                jQuery(this).parent().children('.txt-don-gia-tru-tien-hang-no-cu').val('');
                            }
                        });
                        jQuery('.txt-don-gia-tru-tien-hang-no-cu').keyup(function() {
                            var val = currencyToNumber(jQuery(this).val());
                            if (isNaN(val)) {
                                jQuery(this).val('');
                            }
                            else {
                                jQuery(this).val(numberToCurrency(val));
                            }
                        });
                    }
                }
            });
            jQuery.ajax({
                type: "POST",
                data: {ma_khach_hang : ma_khach_hang},
                url: url + 'san_pham/ban_hang/phieu_ban_hang/load_hang_no_bht',
                async: false,
                success: function(data) {
                    if (data != '') {
                        jQuery('#form-item-hang-no-bht-wrapper').html(data);
                        jQuery('#hnbht-fs').show();
                    }
                }
            });
        }
    });
    jQuery('#load-data-khuyen-mai').click(function() {
        if (load_data_khach_hang) {
            var ma_hang_mua = '';
            var str_id = '';
            var so_luong ='';
            var ma_khach_hang = jQuery('#txt-ma-khach-hang').val();
            var loai_khach_hang = jQuery('#txt-loai-khach-hang').val();
            jQuery('.txt-ten-hang-mua').each(function() {
                var ten_hang_mua = jQuery(this).val();
                var id = jQuery(this).attr('id');
                if (ten_hang_mua != '') {
                    var last_b = ten_hang_mua.lastIndexOf('[') + 1;
                    var last_e = ten_hang_mua.lastIndexOf(']');
                    if (last_b >= 1 && last_e >= 1 && last_e >= last_b + 6) {
                        id = id.replace("txt-ten-hang-mua-", "");
                        if (str_id) {
                            str_id += ",";
                            ma_hang_mua += ",";
                        }
                        str_id += id;
                        ma_hang_mua += ten_hang_mua.substring(last_b, last_e);
                    }
                }
            });
            jQuery('.txt-so-luong-hang-mua').each(function() {
                 var so_luong_hang_mua = jQuery(this).val();
                    if (so_luong_hang_mua != '') {
                    so_luong+=so_luong_hang_mua;
                    so_luong+=",";
                    }
            });

            if (ma_hang_mua.length) {
                jQuery.ajax({
                    type: "POST",
                    data: {ma_hang_mua : ma_hang_mua, str_id : str_id, ma_khach_hang : ma_khach_hang, loai_khach_hang : loai_khach_hang},
                    url: url + 'san_pham/ban_hang/phieu_ban_hang/load_khuyen_mai',
                    async: false,
                    success: function(data) {
//                        if (data.length) {
                        for (key in data) {
//                                alert(key);
                            jQuery('#lbl-don-gia-hang-mua-' + key).html(numberToCurrency(data[key][0]));
                            jQuery('#txt-don-gia-hang-mua-' + key).val(data[key][0]);
                            var so_luong = jQuery('#txt-so-luong-hang-mua-' + key).val();
                            if (so_luong && !isNaN(so_luong)) {
                                if (parseInt(so_luong) <= parseInt(data[key][1])) {
                                    jQuery('#lbl-kiem-tra-so-luong-' + key).html('Đủ hàng');
                                    jQuery('#txt-kiem-tra-so-luong-' + key).val('Đủ hàng');
                                }
                                else {
                                    jQuery('#lbl-kiem-tra-so-luong-' + key).html('Thiếu hàng');
                                    jQuery('#txt-kiem-tra-so-luong-' + key).val('Thiếu hàng');
                                }
                                jQuery('#lbl-thanh-tien-hang-mua-' + key).html(numberToCurrency(data[key][0] * so_luong));
                                jQuery('#txt-thanh-tien-hang-mua-' + key).val(data[key][0] * so_luong);
                            }
                            jQuery('#lbl-mua-tong-thang-' + key).html(data[key][2]);
                            jQuery('#txt-mua-tong-thang-' + key).val(data[key][2]);
                            jQuery('#lbl-km-tong-thang-' + key).html(data[key][3]);
                            jQuery('#txt-km-tong-thang-' + key).val(data[key][3]);
                        }
//                        }
                    }
                });
                jQuery.ajax({
                    type: "POST",
                    data: {so_luong : so_luong,ma_hang_mua : ma_hang_mua,ma_khach_hang : ma_khach_hang},
                    url: url + 'san_pham/ban_hang/phieu_ban_hang/tong_khuyen_mai',
                    async: false,
                    success: function(data) {
                        if (data != '') {
                            jQuery('#load-data-tong-khuyen-mai-wrapper').html(data);
                        }
                    }
                });
            }
        }
        else {
            alert('Chưa lấy dữ liệu khách hàng');
        }
    });
    jQuery('#add-more-hang-mua').click(function() {
        ihm++;
        var html = '<tr class="odd">' +
            '<td>' +
            '<input type="text" autocomplete="OFF" class="form-text form-autocomplete text txt-ten-hang-mua" name="ten_hang_mua[' + ihm + ']" id="txt-ten-hang-mua-' + ihm + '" />' +
            '</td>' +
            '<td>' +
            '<input type="text" class="form-text text txt-so-luong-hang-mua" name="so_luong_hang_mua[' + ihm + ']" id="txt-so-luong-hang-mua-' + ihm + '" autocomplete="OFF" />' +
            '</td>' +
            '<td>' +
            '<input type="button" class="field-remove-submit form-submit" value="X" id="field-remove-hang-mua-submit-' + ihm + '">' +
            '</td>' +
            '<td>' +
            '<label id="lbl-kiem-tra-so-luong-' + ihm + '" class="data-text"></label>' +
            '<input type="hidden" value="" name="kiem_tra_so_luong[' + ihm + ']" id="txt-kiem-tra-so-luong-' + ihm + '" class="txt-hidden" />' +
            '</td>' +
            '<td>' +
            '<label id="lbl-don-gia-hang-mua-' + ihm + '" class="data-text"></label>' +
            '<input type="hidden" value="" name="don_gia_hang_mua[' + ihm + ']" id="txt-don-gia-hang-mua-' + ihm + '" class="txt-hidden" />' +
            '</td>' +
            '<td>' +
            '<label id="lbl-thanh-tien-hang-mua-' + ihm + '" class="data-text"></label>' +
            '<input type="hidden" value="" name="thanh_tien_hang_mua[' + ihm + ']" id="txt-thanh-tien-hang-mua-' + ihm + '" class="txt-hidden" />' +
            '</td>' +
            '<td>' +
            '<input type="text" class="form-text text txt-so-luong-khuyen-mai" name="so_luong_khuyen_mai[' + ihm + ']" id="txt-so-luong-khuyen-mai-' + ihm + '" autocomplete="OFF" />' +
            '</td>' +
            '<td>' +
            '<input id="chk-tru-tien-khuyen-mai-' + ihm + '" type="checkbox" value="' + ihm + '" class="form-checkbox checkbox chk-tru-tien-khuyen-mai tru-tien" name="tru_tien_khuyen_mai[' + ihm + ']" /> ' +
            '</td>' +
            '</tr>';
        jQuery('#form-item-hang-mua-wrapper table.tableheader-processed tbody').append(html);
        var input = jQuery('#txt-ten-hang-mua-' + ihm)
            .attr('autocomplete', 'OFF')
            .attr('aria-autocomplete', 'list');
        jQuery(input[0].form).submit(Drupal.autocompleteSubmit);
        input.parent()
            .attr('role', 'application')
            .append(jQuery('<span class="element-invisible" aria-live="assertive"></span>')
            .attr('id', input.attr('id') + '-autocomplete-aria-live')
        );
        var auto_path = new Drupal.ACDB(jQuery('#txt-ten-hang-mua-1-autocomplete').val());
        new Drupal.jsAC(input, auto_path);
        jQuery('#txt-ten-hang-mua-' + ihm).change(function() {
            jQuery(this).parent().parent().find('.txt-hidden').val('');
            jQuery(this).parent().parent().find('.data-text').html('');
        });
        jQuery('#field-remove-hang-mua-submit-' + ihm).click(function() {
            jQuery(this).parent().parent().remove();
        });
        jQuery('#chk-tru-tien-khuyen-mai-' + ihm).click(function() {
            if (jQuery(this).is(':checked')) {
                jQuery(this).parent().children('.txt-don-gia-tru-tien-khuyen-mai').show();
            }
            else {
                jQuery(this).parent().children('.txt-don-gia-tru-tien-khuyen-mai').hide();
                jQuery(this).parent().children('.txt-don-gia-tru-tien-khuyen-mai').val('');
            }
        });
        jQuery('#txt-don-gia-tru-tien-khuyen-mai-' + ihm).keyup(function() {
            var val = currencyToNumber(jQuery(this).val());
            if (isNaN(val)) {
                jQuery(this).val('');
            }
            else {
                jQuery(this).val(numberToCurrency(val));
            }
        });
    });
    jQuery('#add-more-bao-hanh-truoc').click(function() {
        ibht++;
        var html = '<tr class="odd">' +
            '<td>' +
            '<input type="text" autocomplete="OFF" class="form-text form-autocomplete text txt-ten-hang-bao-hanh-truoc" name="ten_hang_bao_hanh_truoc[]" id="txt-ten-hang-bao-hanh-truoc-' + ibht + '" />' +
            '</td>' +
            '<td>' +
            '<input type="text" class="form-text text txt-so-luong-hang-bao-hanh-truoc" name="so_luong_hang_bao_hanh_truoc[]" id="txt-so-luong-hang-bao-hanh-truoc-' + ibht + '" autocomplete="OFF" />' +
            '</td>' +
            '<td>' +
            '<input type="button" class="field-remove-submit form-submit" value="X" id="field-remove-hang-bao-hanh-truoc-submit-' + ibht + '" />' +
            '</td>' +
            '</tr>';
        jQuery('#form-item-bao-hanh-truoc-wrapper table.tableheader-processed tbody').append(html);
        var input = jQuery('#txt-ten-hang-bao-hanh-truoc-' + ibht)
            .attr('autocomplete', 'OFF')
            .attr('aria-autocomplete', 'list');
        jQuery(input[0].form).submit(Drupal.autocompleteSubmit);
        input.parent()
            .attr('role', 'application')
            .append(jQuery('<span class="element-invisible" aria-live="assertive"></span>')
            .attr('id', input.attr('id') + '-autocomplete-aria-live')
        );
        var auto_path = new Drupal.ACDB(jQuery('#txt-ten-hang-bao-hanh-truoc-1-autocomplete').val());
        new Drupal.jsAC(input, auto_path);
        jQuery('#field-remove-hang-bao-hanh-truoc-submit-' + ibht).click(function() {
            jQuery(this).parent().parent().remove();
        });
    });
});

function check_lap_phieu() {
    if (!load_data_khach_hang) {
        alert('Chưa lấy dữ liệu khách hàng');
    }
    else {
        if (confirm('Sau khi nhập bạn sẽ không thể sửa dữ liệu!\n\rBạn có chắc đã nhập đúng số liệu thành phẩm ?')) {
//            jQuery('#submit').click();
            return true;
        }
    }
    return false;
}
function tong_thang() {
    jQuery('#tong-thang-dialog').dialog({
        autoOpen: false,
        title: 'Chi tiết mua trong tháng',
        modal: true,
        width: 600,
        buttons: {
            Đóng: function() {
                jQuery(this).dialog('close');
            }
        }
    });
    jQuery('#tong-thang-dialog').dialog('open');
}
function tong_km_thang() {
    jQuery('#tong-km-thang-dialog').dialog({
        autoOpen: false,
        title: 'Chi tiết khuyến mại trong tháng',
        modal: true,
        width: 600,
        buttons: {
            Đóng: function() {
                jQuery(this).dialog('close');
            }
        }
    });
    jQuery('#tong-km-thang-dialog').dialog('open');
}
